package com.joint.base.util;


import com.fz.us.base.util.CommonUtil;
import com.fz.us.base.util.LogUtil;
import com.joint.base.entity.system.Sms;
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 短信发送器，文字内部不要包含签名，并且按照模板来实现
 * */
public class SMSUtil {
	public static Logger logger = LoggerFactory.getLogger(SMSUtil.class);

	public static boolean SENDOPEN = false;	//是否开启短信发送
	
	//GET /Services/MsgSend.asmx/SendMsg?userCode=string&userPass=string&DesNo=string&Msg=string&Channel=string HTTP/1.1
	//Host: 121.199.48.186:1210
	
	/*public static String host = "yes.itissm.com";
	public static String url = "/api/MsgSend.asmx/SendMsg";
	public static int port = 80;
	public static String accountId = "shjd";	//
	public static String password = "shjd5858";
	*/
	public static String host = "121.199.48.186";
	public static String url = "/Services/MsgSend.asmx/SendMsg";
	public static int port = 1210;
    public static String accountId = "shjdcf";	//
    public static String password = "shjdcf1818";
	public static String channel = "78";	//企业代码 服务器代码 10657109082938001 002
	public static String title = "【泛在泛销】";
	
	private static final long RECONNECT_TIME = 1 * 1000;
    private static int closedCount = 0;
    
	/**
	 * 
	 * 
	 * */
	/*public static String httpSend(Sms sms) throws Exception{
		if(SENDOPEN){
			String phoneNumber = sms.getPhone();
			String msgText = sms.getMsg() + title;
			
			HttpClient httpClient = new DefaultHttpClient();  
		    String param = "userCode="+accountId+"&userPass="+password+"&DesNo="+phoneNumber+"&Msg="+msgText+"&Channel="+channel;
			URI uri = URIUtils.createURI("http", host, port, url, param, null);  
			HttpUriRequest request = new HttpGet(uri);
			HttpResponse response = httpClient.execute(request);
			// 从response中取出HttpEntity对象
			HttpEntity entity = response.getEntity();
			// 取出服务器返回的数据流
			InputStream stream = entity.getContent();
			// 以任意方式操作数据流stream
			System.out.println(InputStreamUtils.InputStreamTOString(stream));
			return InputStreamUtils.InputStreamTOString(stream);
		}else{
			return null;
		}
	}*/

	public static String httpSend(Sms sms) throws Exception{
		String phoneNumber = sms.getPhone();
		if(CommonUtil.isMobile(phoneNumber)){
			String msgText = sms.getMsg() + title;
			String param = "userCode="+accountId+"&userPass="+password+"&DesNo="+phoneNumber+"&Msg="+msgText+"&Channel="+channel;
			String uri = "http://" + host + ":" + port + url + "?"+param;
			LogUtil.info(uri);
			HttpUriRequest httpGet = new HttpGet(uri);
			CloseableHttpResponse response = HttpClientUtil.getHttpClient().execute(httpGet);
			String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
			return responseContent;
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		Sms sms = new Sms("123456（注册验证码），请在15分钟内提交验证。", "18317170481", null);
		System.out.println(httpSend(sms));

	}
    
	//私有的默认构造子
    private SMSUtil() {}
 

    public static String getSmsText(){
    	
    	return "";
    }
}
